package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //校验参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmSensitive wmSensitive = getById(id);
        wmSensitiveMapper.deleteById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getList(SensitiveDto dto) {
        //校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> qw = new LambdaQueryWrapper<>();
        //按照创建时间倒序查询
        qw.orderByDesc(WmSensitive::getCreatedTime);
        //按照敏感词名称模糊查询
        qw.like(WmSensitive::getSensitives, dto.getName());
        page = page(page, qw);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    /**
     * 保存敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult getSave(WmSensitive wmSensitive) {
        //校验参数
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String sensitives = wmSensitive.getSensitives();
        if (sensitives != null) {


            LambdaQueryWrapper<WmSensitive> qw = new LambdaQueryWrapper<>();
            qw.eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
            List<WmSensitive> list = list(qw);
            if (list.size() > 0 || CollectionUtils.isNotEmpty(list)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该敏感词已存在");

            } else {
                wmSensitiveMapper.insert(wmSensitive);
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult getUpdate(WmSensitive wmSensitive) {
        //校验数据
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String sensitives = wmSensitive.getSensitives();
        if (sensitives == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<WmSensitive> qw = new LambdaQueryWrapper<>();
        qw.eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        List<WmSensitive> list = list(qw);
        if (list.size() > 0 ||  CollectionUtils.isNotEmpty(list) ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "修改的敏感词数据库已存在");
        }
        WmSensitive sensitive = new WmSensitive();
        sensitive.setSensitives(wmSensitive.getSensitives());
        sensitive.setCreatedTime(wmSensitive.getCreatedTime());
        sensitive.setId(wmSensitive.getId());
        wmSensitiveMapper.updateById(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
